<?php
session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Đặt hàng</title>
</head>
<body>


<?php
include("kn.php");
require("check_userlogin.php");
if(!isset($_POST["ngay"])&&!isset($_POST["diadiem"]))
{
?>
<script type="text/javascript" src="script/check.js"></script>
<?php
}
else
{
$lenh = "select * from khachhang where user = '".$_SESSION["user"]."'";
$kq = mysql_query($lenh);
$row = mysql_fetch_assoc($kq);
$user = $row[user];
$makh = $row[Makhachhang];
$thoidiemdathang = date("Y-m-d");
$thoidiemgiaohang = $_POST["nam"]."-".$_POST["thang"]."-".$_POST["ngay"];
$lenhthemdonhang = "insert into donhang(makhachhang,thoidiemdathang,thoidiemgiaohang,diadiemgiaohang,tennguoinhan)values('$makh','$thoidiemdathang','$thoidiemgiaohang','".$_POST["diadiem"]."','".$_SESSION["user"]."')";
$kqthemdonhang = mysql_query($lenhthemdonhang);
if(isset($kqthemdonhang))
{
$lenh2 = "select * from donhang order by idDH desc";
$kq2 = mysql_query($lenh2);
$n2 = mysql_num_rows($kq2);
if($n2 > 0)
{
$row2 =  mysql_fetch_assoc($kq2);
$idDH = $row2[idDH];
	for($i = 1; $i <= $_SESSION["tongsp"];$i++)
	{
	$lenhthemchitiet = "insert into donhangchitiet(Malaptop,Soluong,Dongia,idDH)values('".$_SESSION["Malaptop".$i]."','".$_SESSION["Soluong".$i]."','".$_SESSION["Dongia".$i]."','$idDH')";
		
		$kqthemchitiet = mysql_query($lenhthemchitiet);
	}
}

}

for($k = 1; $k <=$_SESSION["tongsp"];$k++)
{
session_unregister("Malaptop".$k);
session_unregister("Tenlaptop".$k);
session_unregister("Soluong".$k);
session_unregister("Dongia".$k);
}
$_SESSION["tongsp"]=0;
echo"<script>
alert('Đặt hàng thành công!');
location.href='index.php';
</script>";
}

?>
<form id="frmdathang" name="frmdathang" method="post" action="">
  <p>Xin chào: <?php echo $_SESSION["user"];  ?> </p>
  <p>Xin vui lòng điền đầy đủ thông tin để đặt hàng:</p>
  <table width="550" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#FF9933" style="border-collapse:collapse">
    <tr>
      <td width="219">Thời điểm giao hàng</td>
      <td width="331"><label for="ngay">Ngày</label>
        <select name="ngay" id="ngay">
        <?php
		  for($i = 1; $i <= 31; $i++)
		  {
		  	if($i == date(d)	)  
		  echo"<option selected>$i</option>";
		  else
		   echo"<option>$i</option>";
		  }
		  ?>
       
      </select> <label for="thang">Tháng</label>
      <select name="thang" id="thang">
      <?php
		for($i = 1; $i <= 12; $i++)
		{
			if($i==date("m"))
			echo " <option selected>$i</option>";
			else
			echo " <option>$i</option>";
		}
		
		?>
      </select> <label for="nam">Năm</label>
      <select name="nam" id="nam">
       <?php
		for($i = 1975; $i <= 2015; $i++)
		{
			if($i==date("Y"))
			echo " <option selected>$i</option>";
			else
			echo " <option>$i</option>";
		}
		?>
      </select>
      </td>
    </tr>
    <tr>
      <td>Địa điểm giao hàng</td>
      <td><label for="diadiem"></label>
      <input name="diadiem" type="text" id="diadiem" size="40" /></td>
    </tr>
  </table>
  <p align="center">
    <input type="submit" name="dathang" id="dathang" value="Đặt hàng" />
  </p>
</form>
</body>
</html>